Telegram Group & Telegram Channel
Сегодня я хочу показать вам простой, но очень полезный приём для работы с массивами в PHP — использование функции array_column().

Очень часто бывает задача: из многомерного массива "вытащить" только один столбец данных. Например, у вас есть массив пользователей:


$users = [
['id' => 1, 'name' => 'Иван', 'email' => '[email protected]'],
['id' => 2, 'name' => 'Ольга', 'email' => '[email protected]'],
['id' => 3, 'name' => 'Павел', 'email' => '[email protected]'],
];


И нужно получить только список всех email'ов. Многие делают это через foreach, но есть более элегантное решение:


$emails = array_column($users, 'email');


Результат будет таким:


[
'[email protected]',
'[email protected]',
'[email protected]'
]


А ещё array_column() позволяет указать третий параметр — ключ массива. Например, сделать email по id:


$emailsById = array_column($users, 'email', 'id');


И тогда получится:


[
1 => '[email protected]',
2 => '[email protected]',
3 => '[email protected]'
]


Это удобный способ быстрого преобразования данных без лишнего кода.

Пользуетесь ли вы array_column() в своих проектах? Или предпочитаете что-то другое? Напишите в комментариях!

👉 @php_lib



tg-me.com/php_lib/743
Create:
Last Update:

Сегодня я хочу показать вам простой, но очень полезный приём для работы с массивами в PHP — использование функции array_column().

Очень часто бывает задача: из многомерного массива "вытащить" только один столбец данных. Например, у вас есть массив пользователей:


$users = [
['id' => 1, 'name' => 'Иван', 'email' => '[email protected]'],
['id' => 2, 'name' => 'Ольга', 'email' => '[email protected]'],
['id' => 3, 'name' => 'Павел', 'email' => '[email protected]'],
];


И нужно получить только список всех email'ов. Многие делают это через foreach, но есть более элегантное решение:


$emails = array_column($users, 'email');


Результат будет таким:


[
'[email protected]',
'[email protected]',
'[email protected]'
]


А ещё array_column() позволяет указать третий параметр — ключ массива. Например, сделать email по id:


$emailsById = array_column($users, 'email', 'id');


И тогда получится:


[
1 => '[email protected]',
2 => '[email protected]',
3 => '[email protected]'
]


Это удобный способ быстрого преобразования данных без лишнего кода.

Пользуетесь ли вы array_column() в своих проектах? Или предпочитаете что-то другое? Напишите в комментариях!

👉 @php_lib

BY Библиотека PHP программиста 👨🏼‍💻👩‍💻


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/php_lib/743

View MORE
Open in Telegram


Библиотека PHP программиста ‍‍ Telegram | DID YOU KNOW?

Date: |

Telegram and Signal Havens for Right-Wing Extremists

Since the violent storming of Capitol Hill and subsequent ban of former U.S. President Donald Trump from Facebook and Twitter, the removal of Parler from Amazon’s servers, and the de-platforming of incendiary right-wing content, messaging services Telegram and Signal have seen a deluge of new users. In January alone, Telegram reported 90 million new accounts. Its founder, Pavel Durov, described this as “the largest digital migration in human history.” Signal reportedly doubled its user base to 40 million people and became the most downloaded app in 70 countries. The two services rely on encryption to protect the privacy of user communication, which has made them popular with protesters seeking to conceal their identities against repressive governments in places like Belarus, Hong Kong, and Iran. But the same encryption technology has also made them a favored communication tool for criminals and terrorist groups, including al Qaeda and the Islamic State.

Telegram Be The Next Best SPAC

I have no inside knowledge of a potential stock listing of the popular anti-Whatsapp messaging app, Telegram. But I know this much, judging by most people I talk to, especially crypto investors, if Telegram ever went public, people would gobble it up. I know I would. I’m waiting for it. So is Sergei Sergienko, who claims he owns $800,000 of Telegram’s pre-initial coin offering (ICO) tokens. “If Telegram does a SPAC IPO, there would be demand for this issue. It would probably outstrip the interest we saw during the ICO. Why? Because as of right now Telegram looks like a liberal application that can accept anyone - right after WhatsApp and others have turn on the censorship,” he says.

Библиотека PHP программиста ‍‍ from ms


Telegram Библиотека PHP программиста 👨🏼‍💻👩‍💻
FROM USA